package com.zxy.controller;

import com.google.zxing.WriterException;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import com.zxy.utils.QRCodeFactory;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.OutputStream;

@WebServlet("/QRCode")
public class QRCode extends HttpServlet {
    private static Logger logger = LogManager.getLogger(QRCode.class);
    @Override
    public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        response.setHeader("Content-Type","image/jpeg");
        response.setHeader("pragma", "no-cache");
        response.setHeader("cache-demo", "no-cache");
        response.setHeader("expires", "0");
        OutputStream os=response.getOutputStream();
        String content="欢迎访问本站！";
        if (request.getParameter("content")!=null&&!request.getParameter("content").equals("")){
            
            content=request.getParameter("content");
        }
        int[] size=new int[]{200,200};
        String format = "jpg";
        try {
            new QRCodeFactory().CreateQrImage(content,format, os,size);
        } catch (WriterException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }


}
